<?

class BDS {

    protected static $db = false;
    protected static $table = false;
    protected static $pid_name = false;
    static $id_name = false;

    final static function Init() {
        self::$db = &$GLOBALS['MAIN']->db;
    }

    final static function GetFullStructure($use_elements = false) {
        $types = BDSTypes::GetList(array('site_id' => $GLOBALS['MAIN']->site->id));
        foreach ($types as $i => $type) {
            $structures = BDSStructures::GetList(array('type_id' => $type['id']));
            foreach ($structures as $j => $structure) {
                $blocks = BDSBlocks::GetList(array('structure_id' => $structure['id']));
                if ($use_elements) {
                    foreach ($blocks as $k => $block) {
                        $sections = BDSSections::GetList(array('block_id' => $block['id'], 'type' => 'S'));
                        $sections = Tree($sections, 'id', 'section_id', 'sections', $use_elements);
                        $blocks[$k]['sections'] = $sections;
                    }
                }
                $structures[$j]['blocks'] = $blocks;
            }
            $types[$i]['structures'] = $structures;
        }
        return $types;
    }

    public static function GetList($filter = array(), $sort = array(), $fields = array(), $use_empty = true) {
        $process = self::$db->select()->table(static::$table);

        foreach ($filter as $logic_field => $cond_value) {
            $logic = DB_LOGIC_AND;
            $field = $logic_field;
            $cond = DB_COND_EQUAL;
            $value = $cond_value;
            if (preg_match("#(.+):(.+)#", $logic_field, $l)) {
                $logic = $l[1];
                $field = $l[2];
            }
            if (preg_match("#(.+):(.+)#", $cond_value, $m)) {
                $cond = $m[1];
                $value = $m[2];
            }
            $process->cond($field, $value, $cond, $logic);
        }

        if (empty($fields)) {
            $process->field();
        } else {
            foreach ($fields as $field => $alias) {
                if (is_numeric($field)) {
                    $field = $alias;
                    $alias = false;
                }
                $process->field($field, $alias);
            }
        }

        foreach ($sort as $sort_field => $direction) {
            $process->order($sort_field, $direction);
        }
        $result = $process->execute();
        $data = $result->GetList();
        if ($use_empty && !$data) {
            $data = array();
        }
        return $data;
    }

    public static function GetListSql($filter = array(), $sort = array(), $fields = array(), $use_empty = true) {
        $process = self::$db->select()->table(static::$table);

        foreach ($filter as $logic_field => $cond_value) {
            $logic = DB_LOGIC_AND;
            $field = $logic_field;
            $cond = DB_COND_EQUAL;
            $value = $cond_value;
            if (preg_match("#(.+):(.+)#", $logic_field, $l)) {
                $logic = $l[1];
                $field = $l[2];
            }
            if (preg_match("#(.+):(.+)#", $cond_value, $m)) {
                $cond = $m[1];
                $value = $m[2];
            }
            $process->cond($field, $value, $cond, $logic);
        }

        if (empty($fields)) {
            $process->field();
        } else {
            foreach ($fields as $field => $alias) {
                if (is_numeric($field)) {
                    $field = $alias;
                    $alias = false;
                }
                $process->field($field, $alias);
            }
        }

        foreach ($sort as $sort_field => $direction) {
            $process->order($sort_field, $direction);
        }
        $result = $process->GetSql();
        
        return $result;
    }

    public static function GetById($id) {
        $result = static::GetList(array("id" => $id));
        return $result[0];
    }
    public static function GetByCode($code) {
        $result = static::GetList(array("code" => $code));
        if($result){
            return $result[0];
        }else{
            return $result;
        }
    }

    public static function Create($data) {
        $result = false;
        $fields = array_keys($data);
        $process = self::$db->insert()->table(static::$table)->field($fields)->values($data);
        $result = $process->execute();
        return $result->IsTrue() == true;
    }

    public static function CreateSql($data) {
        $result = false;
        $fields = array_keys($data);
        $process = self::$db->insert()->table(static::$table)->field($fields)->values($data);
        $result = $process->GetSql() . ";\n";
        return $result;
    }

    public static function Delete($data) {
        $process = self::$db->delete()->table(static::$table)->cond("id", $data);
        $result = $process->execute();
        return $result->IsTrue() == true;
        //return $result;
    }

    public static function DeleteSql($data) {
        $process = self::$db->delete()->table(static::$table)->cond("id", $data);
        $result = $process->GetSql() . ";\n";
        return $result;
    }

    public static function Update($id, $data) {

        $process = self::$db->update()->table(static::$table);
        foreach ($data as $key => $value) {
            $process->set($key, $value);
        }
        $process->cond('id', $id);
        $result = $process->execute();
        return $result->IsTrue() == true;
    }

    public static function UpdateSql($id, $data) {

        $process = self::$db->update()->table(static::$table);
        foreach ($data as $key => $value) {
            $process->set($key, $value);
        }
        $process->cond('id', $id);
        $result = $process->GetSql();
        return $result;
    }

    public static function NextId() {
        $sql = "SHOW TABLE STATUS LIKE '" . static::$table . "'";
        $result = self::$db->native($sql)->execute();
        return $result->GetList()[0]['Auto_increment'];
    }

}

class BDSTypes extends BDS {

    protected static $table = "bds__types";
    protected static $pid_name = "site_id";
    static $id_name = "type_id";

}

class BDSStructures extends BDS {

    protected static $table = "bds__structures";
    protected static $pid_name = "type_id";
    static $id_name = "structure_id";

}

class BDSBlocks extends BDS {

    protected static $table = "bds__blocks";
    protected static $pid_name = "structure_id";
    static $id_name = "block_id";

    public static function GetList($filter = array(), $sort = array(), $fields = array(), $use_empty = true) {
        $blocks = parent::GetList($filter, $sort, $fields, $use_empty);
        foreach ($blocks as $i => $block) {
            $blocks[$i]['section_fields'] = BDSFields::GetList(array('block_id' => $block['id'], 'relation' => 'S'));
            $blocks[$i]['element_fields'] = BDSFields::GetList(array('block_id' => $block['id'], 'relation' => 'E'));
        }
        return $blocks;
    }
}

class BDSSections extends BDS {

    protected static $table = "bds__elements";
    protected static $pid_name = "block_id";
    static $id_name = "parent_id";

    public static function GetList($filter = array(), $sort = array(), $fields = array(), $use_empty = true) {
        $filter["type"] = "S";
        return parent::GetList($filter, $sort, $fields, $use_empty);
    }

}

class BDSElements extends BDS {

    protected static $table = "bds__elements";
    protected static $pid_name = "block_id";
    static $id_name = "parent_id";

    public static function GetList($filter = array(), $sort = array(), $fields = array(), $use_empty = true) {
        $filter["type"] = "E";
        return parent::GetList($filter, $sort, $fields, $use_empty);
    }

}

class BDSRecords extends BDS {

    protected static $table = "bds__elements";
    protected static $pid_name = "block_id";
    static $id_name = "parent_id";

}

class BDSFields extends BDS {

    protected static $table = "fld__fields";
    protected static $pid_name = "block_id";
    static $id_name = "field_id";

    public static function GetType($id = false) {
        $process = parent::$db->select()->table('fld__types')->field();
        if ($id) {
            $process->cond('id', $id);
        }
        $result = $process->execute();
        $data = $result->GetList();
        if ($id) {
            return $data[0];
        } else {
            return $data;
        }
    }

}

class BDSFieldValues extends BDS {

    protected static $table = "fld__values";
    protected static $pid_name = "field_id";
    static $id_name = "id";

}
